﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Client
{
   public class PenduGame
   {
      #region Methods

      public List<string> AllWords
      {
         get
         {
            List<string> ls = new List<string>();

            ls.Add("DECIMAL");
            ls.Add("TELEPHONE");
            ls.Add("ELEPHANT");
            ls.Add("MAMMOUTH");
            ls.Add("DINOSAURE");
            ls.Add("AVION");
            ls.Add("AVIATEUR");
            ls.Add("DIRIGEABLE");
            ls.Add("BALLON");
            ls.Add("EXPERT");
            ls.Add("PENDU");
            ls.Add("TABLEAU");
            ls.Add("CINEMA");
            ls.Add("BAGUETTE");
            ls.Add("CHINOISE");
            ls.Add("JAPONAISE");
            ls.Add("NATATION");
            ls.Add("VIOLON");
            ls.Add("XYLOPHONE");
            ls.Add("CHAMPIONNAT");
            ls.Add("AMATEUR");
            ls.Add("WALIBI");
            ls.Add("WAZABI");
            ls.Add("YEN");
            ls.Add("YETI");
            ls.Add("ZOO");
            ls.Add("OXYMORE");
            ls.Add("OXYGENE");
            ls.Add("RESULTAT");
            ls.Add("TRUCAGE");
            ls.Add("KEVLAR");
            ls.Add("KIOSQUE");
            ls.Add("CANOE");
            ls.Add("KAYAK");
            ls.Add("ULTIME");
            ls.Add("OREILLE");
            ls.Add("OSEILLE");
            ls.Add("AIMANT");
            ls.Add("HYDROSPEED");
            ls.Add("BONJOUR");
            ls.Add("PATIN");
            ls.Add("CONSTIPEE");
            ls.Add("BADMINTON");
            ls.Add("MACHISME");
            ls.Add("PHALOCRATE");
            ls.Add("PHALANGE");
            ls.Add("PANTALON");
            ls.Add("PHARE");
            ls.Add("FISTULE");
            ls.Add("MEGALO");
            ls.Add("BUREAU");
            ls.Add("ELODIE");
            ls.Add("DELATION");
            ls.Add("AUGMENTATION");
            ls.Add("UTOPIE");
            ls.Add("VACANCES");
            ls.Add("WEEKEND");
            ls.Add("PLAISIR");
            ls.Add("VOYAGE");
            ls.Add("CORSE");
            ls.Add("INSOLATION");
            ls.Add("DESOLATION");
            ls.Add("INTROSPECTION");
            ls.Add("CAMEMBERT");
            ls.Add("ROQUEFORT");
            ls.Add("BEAUFORT");
            ls.Add("ETAGERE");
            ls.Add("ASCENSEUR");
            ls.Add("PANNE");

            return ls;
         }
      }

      public string FindWord()
      {
         try
         {
            Random rand = new Random();
            int nb = rand.Next(0, this.AllWords.Count - 1);

            this.ActualWord = this.AllWords[nb];
         }
         catch
         {
            this.ActualWord = "PLANTAGE";
         }

         return this.ActualWord;
      }

      #endregion

      #region Properties

      /// <summary>
      /// Obtient le mot actuel
      /// </summary>
      public string ActualWord { get; set; }

      private List<string> _allLetters;
      /// <summary>
      /// Obtient toutes les lettres jouées
      /// </summary>
      public List<string> AllLetters
      {
         get
         {
            if (_allLetters == null)
               _allLetters = new List<string>();

            return _allLetters;
         }
      }

      public int Errors;
      public bool WinGame = false;

      #endregion

      #region Methods

      /// <summary>
      /// Affiche le mot actuel
      /// </summary>
      /// <returns></returns>
      public string DisplayWord()
      {
         string str = string.Empty;

         bool isOk = true;

         foreach (char c in this.ActualWord.ToCharArray())
         {
            if (this.AllLetters.Contains(c.ToString()))
               str = string.Concat(str, c.ToString());
            else
            {
               isOk = false;
               str = string.Concat(str, "_");
            }
            str = string.Concat(str, " ");
         }

         if (isOk)
            this.WinGame = true;

         return str;
      }

      public string DisplayResult()
      {
         if (this.WinGame)
            return "You win ! You're the best !!! Do you want to start a new game ? Press _Y";

         if (this.Errors == 7)
            return "You lose ! You're a looser !!! Do you want to start a new game ? Press _Y";

         string str = string.Concat("Errors : ", this.Errors.ToString(), "/7 Letters : ");

         foreach (string s in this.AllLetters)
         {
            if (!this.ActualWord.Contains(s))
               str = string.Concat(str, s, " ");
         }

         return str;
      }

      /// <summary>
      /// Ajoute une lettre
      /// </summary>
      /// <param name="value"></param>
      public string AddLetter(string value)
      {
         if (((this.Errors >= 7) || (this.WinGame == true)) && (value == "Y"))
         {
            this.StartPenduGame();
            return string.Empty;
         }

         if (value.Length == 1)
         {
            if (!this.ActualWord.Contains(value))
               this.Errors++;

            this.AllLetters.Add(value);
         }

         return this.DisplayWord();
      }

      public void StartPenduGame()
      {
         this.WinGame = false;
         this.Errors = 0;
         this.AllLetters.Clear();
         this.FindWord();
      }

      #endregion

   }
}
